<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>05_猜数字练习</title>
</head>
<body>
<h3>已生成一个1~100的随机数</h3>
<input type="text" id="input" placeholder="请输入你猜的数字"></input>
<button onclick="guess()">点我校验</button>
<h4>小提示：<span></span></h4>

<script>
    let x = parseInt(Math.random()*100+1);
    console.log(x);
    var count = 0;

    function guess(){
        var num = document.getElementById('input').value;

        if(!/^\d+$/.test(num)){
            alert('请输入数字');
            return;
        }

        let spanE = document.querySelector('span');

        if(num>x){
            spanE.innerHTML = '猜大了';
            count++;
        }else if(num<x){
            spanE.innerHTML = '猜小了';
            count++;
        }else{
            spanE.innerHTML = '恭喜你猜对了';
            alert('你用了'+count+'次');
        }
    }
</script>
</body>
</html>